package de.tum.in.eist.poll.server;

import de.tum.in.eist.poll.pub.shared.Feedback;
import de.tum.in.eist.poll.pub.shared.FeedbackExtended;

import de.tum.in.eist.poll.pub.shared.Lecture;

import de.tum.in.eist.poll.pub.shared.Lecturer;

import de.tum.in.eist.poll.pub.shared.Student;

import de.tum.in.eist.poll.pub.shared.User;

import java.io.IOException;

import java.util.ArrayList;

import java.util.LinkedHashMap;

import java.util.LinkedList;

import java.util.List;
import java.util.Map;

import com.google.appengine.api.datastore.DatastoreService;
import com.google.appengine.api.datastore.DatastoreServiceFactory;
import com.google.appengine.api.datastore.Entity;
import com.google.appengine.api.datastore.Key;
import com.google.appengine.api.datastore.KeyFactory;
import com.google.appengine.api.datastore.Query;
import com.google.gwt.user.client.Window;

public final class Database {
	private static List<Lecture> lectures;
	private static List<User> users;
	private static List<Student> students;
	private static List<Lecturer> lecturers;
	private static Map<Lecture, List<Feedback>> lectureToFeedback = new LinkedHashMap();

	public static void submitFeedbackToServer(FeedbackExtended fb)
			throws IOException {
		int q1 = fb.q1;
		String q2 = fb.q2;
		String q3 = fb.q3;
		Student curUser = fb.getStudent();
		Lecture curLecture = fb.getLecture();
		// if (!feedbackExists(curLecture, curUser)) {
		Key id = KeyFactory.createKey("Feedbacks", curLecture.getLectureID());
		Entity feedback = new Entity("feedback", id);
		feedback.setProperty("user", curUser.getName());
		feedback.setProperty("question1", q1);
		feedback.setProperty("question2", q2);
		feedback.setProperty("question3", q3);

		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		datastore.put(feedback);
		Window.alert("Feedback gespeichert!");
		// } else {
		// Window.alert("Du hast schon ein Feedback zu dieser Lecture abgegeben!");
		// }
	}

	public static boolean feedbackExists(Lecture curLecture, Student curUser) {
		DatastoreService datastore = DatastoreServiceFactory
				.getDatastoreService();
		Key id = KeyFactory.createKey("Feedbacks", curLecture.getLectureID());
		Query query = new Query("Feedback", id).addFilter("user",
				Query.FilterOperator.EQUAL, curUser.getName());
		if (query != null)
			return true;
		else
			return false;
	}

	public static void addFeedbackToLecture(Lecture lecture, Feedback feedback) {
		List feedbackForLecture = (List) lectureToFeedback.get(lecture);
		if (feedbackForLecture == null) {
			feedbackForLecture = new LinkedList();
			feedbackForLecture.add(feedback);
			lectureToFeedback.put(lecture, feedbackForLecture);
		} else {
			feedbackForLecture.add(feedback);
		}
	}

	public static boolean isValidInput(String name, String password) {
		User user = getUser(name);
		if (user != null) {
			return user.getPassword().equals(password);
		}
		return false;
	}

	public static User getUser(String name) {
		if (users == null) {
			users = new LinkedList();
			users.addAll(getStudents());
			users.addAll(getLecturers());
		}

		for (User user : users) {
			if (user.getName().equals(name)) {
				return user;
			}
		}

		return null;
	}

	public static List<Student> getStudents() {
		if (students == null) {
			students = new LinkedList();

			students.add(new Student("Student_1", "new"));
			students.add(new Student("Student_2", "new"));
		}
		return students;
	}

	public static List<Lecturer> getLecturers() {
		if (lecturers == null) {
			lecturers = new ArrayList(2);
			lecturers.add(new Lecturer("Prof", "new"));
			lecturers.add(new Lecturer("Maalej", "new"));
		}
		return lecturers;
	}

	public static List<Lecture> getLectures() {
		if (lectures == null) {
			lectures = new ArrayList(3);
			Lecturer lecturer = (Lecturer) getLecturers().get(0);
			lectures.add(new Lecture("IN0001", "EIST_1", lecturer));
			lectures.add(new Lecture("IN0002", "EIST_2", lecturer));
			lecturer = (Lecturer) getLecturers().get(1);
			lectures.add(new Lecture("IN0003", "EIST_3", lecturer));
		}
		return lectures;
	}

	public static Lecture getLecture(String lectureID) {
		for (Lecture lecture : lectures) {
			if (lecture.getLectureID().equals(lectureID)) {
				return lecture;
			}
		}

		return null;
	}

	public static List<Feedback> getFeedback(Lecture lecture) {
		return ((List) lectureToFeedback.get(lecture));
	}
}
